package ar.com.ford.it.fullEconomics.view.form;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc06Item;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class CostStructureItemForm extends ValidatorForm {
    private String section;
    private String nameFind;
    private String codeFind;
    private String codeCreateUpdate;
    private String nameCreateUpdate;
    private Collection items;

    // For find item combo value
    private String itemCatCodeSelFind;

    // For create/update item combo value
    private String itemCatCodeSel;

    private Collection itemCategories;

    // For economics combo value
    private String economicCodeSel;
    private Collection economics;

    private String createdBy;
    private String createdDate;
    private String proposedBy;
    private String proposedDate;
    private String approvedBy;
    private String approvedDate;
    private String lastModifiedBy;
    private String lastModifiedDate;
    private String screen;
    private Teesc06Item itemSelected;

    public void resetForm() {
        this.section = null;
        this.nameFind = null;
        this.codeFind = null;
        this.codeCreateUpdate = null;
        this.nameCreateUpdate = null;
        this.items = null;
        this.itemCatCodeSelFind = null;
        this.itemCatCodeSel = null;
        this.itemCategories = null;
        this.economicCodeSel = null;
        this.economics = null;
        this.createdBy = null;
        this.createdDate = null;
        this.proposedBy = null;
        this.proposedDate = null;
        this.approvedBy = null;
        this.approvedDate = null;
        this.lastModifiedBy = null;
        this.lastModifiedDate = null;
        this.screen = null;
        this.itemSelected = null;
    }

    public void clearCreateUpdateFields() {
        this.codeCreateUpdate = null;
        this.itemCatCodeSel = "-2";
        this.economicCodeSel = "-2";
        this.nameCreateUpdate = null;
        this.createdBy = null;
        this.proposedBy = null;
        this.approvedBy = null;
        this.lastModifiedBy = null;
        this.createdDate = null;
        this.proposedDate = null;
        this.approvedDate = null;
        this.itemSelected = null;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getCodeFind() {
        return codeFind;
    }

    public void setCodeFind(String codeFind) {
        this.codeFind = codeFind;
    }

    public Collection getItemCategories() {
        return itemCategories;
    }

    public void setItemCategories(Collection itemCategories) {
        this.itemCategories = itemCategories;
    }

    public String getItemCatCodeSelFind() {
        return itemCatCodeSelFind;
    }

    public void setItemCatCodeSelFind(String itemCatCodeSelFind) {
        this.itemCatCodeSelFind = itemCatCodeSelFind;
    }

    public Teesc06Item getItemSelected() {
        return itemSelected;
    }

    public void setItemSelected(Teesc06Item itemSelected) {
        this.itemSelected = itemSelected;
    }

    public String getNameFind() {
        return nameFind;
    }

    public void setNameFind(String nameFind) {
        this.nameFind = nameFind;
    }

    public String getApprovedBy() {
        return approvedBy;
    }

    public void setApprovedBy(String approvedBy) {
        this.approvedBy = approvedBy;
    }

    public String getCodeCreateUpdate() {
        return codeCreateUpdate;
    }

    public void setCodeCreateUpdate(String codeCreateUpdate) {
        this.codeCreateUpdate = codeCreateUpdate;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getEconomicCodeSel() {
        return economicCodeSel;
    }

    public void setEconomicCodeSel(String economicCodeSel) {
        this.economicCodeSel = economicCodeSel;
    }

    public Collection getEconomics() {
        return economics;
    }

    public void setEconomics(Collection economics) {
        this.economics = economics;
    }

    public String getItemCatCodeSel() {
        return itemCatCodeSel;
    }

    public void setItemCatCodeSel(String itemCatCodeSel) {
        this.itemCatCodeSel = itemCatCodeSel;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public String getNameCreateUpdate() {
        return nameCreateUpdate;
    }

    public void setNameCreateUpdate(String nameCreateUpdate) {
        this.nameCreateUpdate = nameCreateUpdate;
    }

    public String getProposedBy() {
        return proposedBy;
    }

    public void setProposedBy(String proposedBy) {
        this.proposedBy = proposedBy;
    }

    public String getScreen() {
        return screen;
    }

    public void setScreen(String screen) {
        this.screen = screen;
    }

    public Collection getItems() {
        return items;
    }

    public void setItems(Collection items) {
        this.items = items;
    }

    /**
     * Setea el Item seleccionado para luego ser modificado, borrado, aprobado,
     * etc.
     * 
     * @param key
     * @param value
     */
    public void setItemMapped(String key, String value) {
        List items = (List) this.getItems();

        this
                .setItemSelected((Teesc06Item) items
                        .get(Integer.parseInt(key) - 1));
    }

    public Teesc06Item getItemForCreate() {
        Teesc06Item item = new Teesc06Item();
        item.setEesc06ItemNameN(this.getNameCreateUpdate());

        return item;
    }

    public Teesc06Item getItemForUpdate(Teesc06Item item) {
        item.setEesc06ItemNameN(this.getNameCreateUpdate());

        return item;
    }

    public void setItemDetails(Teesc06Item item) {
        Calendar aux = Calendar.getInstance();

        this.setCodeCreateUpdate(item.getEesc06ItemNumberK().toString());
        this.setNameCreateUpdate(item.getEesc06ItemNameN());
        this.setItemCatCodeSel(item.getTeesc05Itemcatgry()
                .getEesc05ItemcatgryNumberK().toString());
        if (item.getEesc06ItemIndexedFlagF().equals("1")) {
            this.setEconomicCodeSel(item.getTeese01Economic()
                    .getEese01EconNumberK().toString());
        } else { 
            this.setEconomicCodeSel("-1");
        }
        this.setCreatedBy(item.getEesc06CreateUserC());
        this.setProposedBy(item.getEesc06ItemProposalUserC());
        this.setApprovedBy(item.getEesc06ItemApprovalUserC());
        this.setLastModifiedBy(item.getEesc06LastUpdtUserC());
        if (item.getEesc06CreateS() != null) {
            aux.setTimeInMillis(item.getEesc06CreateS().getTime());
            this.setCreatedDate(FullEconomicsDateUtil.format(aux));
        }
        if (item.getEesc06ItemProposalS() != null) {
            aux.setTimeInMillis(item.getEesc06ItemProposalS().getTime());
            this.setProposedDate(FullEconomicsDateUtil.format(aux));
        }
        if (item.getEesc06ItemApprovalS() != null) {
            aux.setTimeInMillis(item.getEesc06ItemApprovalS().getTime());
            this.setApprovedDate(FullEconomicsDateUtil.format(aux));
        }
        if (item.getEesc06LastUpdtUserC() != null) {
            aux.setTimeInMillis(item.getEesc06LastUpdtS().getTime());
            this.setLastModifiedDate(FullEconomicsDateUtil.format(aux));
        }
    }

    public String getApprovedDate() {
        return approvedDate;
    }

    public void setApprovedDate(String approvedDate) {
        this.approvedDate = approvedDate;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(String lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public String getProposedDate() {
        return proposedDate;
    }

    public void setProposedDate(String proposedDate) {
        this.proposedDate = proposedDate;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        if ("find".equalsIgnoreCase(this.getSection())) {
            if (StringUtils.isNotBlank(this.getCodeFind())) {
                if (this.getCodeFind().matches("[0-9]+")) {
                    try {
                        if (Integer.parseInt(this.getCodeFind()) == 0) {
                            ae
                                    .add(
                                            ActionMessages.GLOBAL_MESSAGE,
                                            new ActionMessage(
                                                    "errors.integergtzero",
                                                    resources
                                                            .getMessage("costStructure.item.label.code")));
                        }
                    } catch (NumberFormatException e) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.invalid",
                                                resources
                                                        .getMessage("costStructure.item.label.code")));
                    }
                } else {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.integergtzero",
                                            resources
                                                    .getMessage("costStructure.item.label.code")));
                }
            }
        } else if ("createUpdate".equalsIgnoreCase(this.getSection())) {
            if (StringUtils.isBlank(this.getNameCreateUpdate())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.required", resources
                                .getMessage("costStructure.item.label.name")));
            }
            if ("-2".equals(this.getItemCatCodeSel())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "error.noSelected.m", resources
                                .getMessage("costStructure.itemCategory")));
            }
            if ("-2".equals(this.getEconomicCodeSel())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "error.noSelected.m", resources
                                .getMessage("economicIndexDef")));
            }
        } else if ("delete".equalsIgnoreCase(this.getSection())) {
            if (this.getItemSelected() == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "error.noSelected.m", resources
                                .getMessage("costStructure.item")));
            }
        }

        return ae;
    }

    public void cleanFindFields() {
        this.itemCatCodeSelFind = "-1";
        this.codeFind = null;
        this.nameFind = null;
    }

}
